home *** CD-ROM | disk | FTP | other *** search
- /*
- * a header of the class XY
- * Copyright (C) 1996, 1997 Kazutaka Hirata <khirata@jove.acs.unt.edu>
- */
-
- #ifndef _XY_H_
- #define _XY_H_
-
- #include <math.h>
- #include <stdlib.h>
-
- #include "common/base.h"
-
- typedef int XYT;
-
- class XY {
- XYT m_x, m_y;
- public:
- XY()
- : m_x(0),
- m_y(0) {}
- XY(const XY& rval)
- : m_x(rval.m_x),
- m_y(rval.m_y) {}
- XY(XYT x, XYT y)
- : m_x(x),
- m_y(y) {}
- void set_x(XYT x) { m_x = x; }
- void set_y(XYT y) { m_y = y; }
- void set(XYT x, XYT y) {
- set_x(x);
- set_y(y);
- }
- XYT x() const { return m_x; }
- XYT y() const { return m_y; }
- void operator=(const XY& src) {
- m_x = src.x();
- m_y = src.y();
- }
- int operator==(const XY& r) const {
- const XY& l = *this;
- return (l.x() == r.x()) && (l.y() == r.y());
- }
- int operator!=(const XY& r) const {
- const XY& l = *this;
- return (l.x() != r.x()) || (l.y() != r.y());
- }
- friend XY operator+(const XY& l, const XY& r) {
- return XY(l.x() + r.x(), l.y() + r.y());
- }
- friend XY operator+(const XY& l, XYT d) {
- return XY(l.x() + d, l.y() + d);
- }
- XY operator+=(const XY& v) {
- m_x += v.x();
- m_y += v.y();
- return *this;
- }
- XY operator+=(XYT d) {
- m_x += d;
- m_y += d;
- return *this;
- }
- friend XY operator-(const XY& l) {
- return XY(- l.x(), - l.y());
- }
- friend XY operator-(const XY& l, const XY& r) {
- return XY(l.x() - r.x(), l.y() - r.y());
- }
- friend XY operator-(const XY& l, XYT d) {
- return XY(l.x() - d, l.y() - d);
- }
- XY operator-=(const XY& v) {
- m_x -= v.m_x;
- m_y -= v.m_y;
- return *this;
- }
- XY operator-=(XYT d) {
- m_x -= d;
- m_y -= d;
- return *this;
- }
- friend XY operator*(const XY& l, double r) {
- return XY(XYT(double(l.x()) * r), XYT(double(l.y()) * r));
- }
- friend XY operator/(const XY& l, int r) {
- return XY(XYT(l.x() / r), XYT(l.y() / r));
- }
- friend XY operator/(const XY& l, double r) {
- return XY(XYT(double(l.x()) / r), XYT(double(l.y()) / r));
- }
- friend int operator<(const XY& l, const XY& r) {
- return (l.m_x < r.m_x) && (l.m_y < r.m_y);
- }
- friend int operator<=(const XY& l, const XY& r) {
- return (l.m_x <= r.m_x) && (l.m_y <= r.m_y);
- }
- friend int operator>(const XY& l, const XY& r) {
- return (l.m_x > r.m_x) && (l.m_y > r.m_y);
- }
- friend int operator>=(const XY& l, const XY& r) {
- return (l.m_x >= r.m_x) && (l.m_y >= r.m_y);
- }
- friend int compare_distance_lt(const XY& p, const XY& q, XYT d);
- friend double get_distance(const XY& p, const XY& q) {
- double dx = p.x() - q.x();
- double dy = p.y() - q.y();
- return sqrt(dx * dx + dy * dy);
- }
- double get_radian(const XY& p) const;
- void rotate(const XY& src, double rad);
- void rotate_90() {
- *this = XY(-y(), x());
- }
- bool is_in_box(const XY& p, const XY& q) const;
-
- friend XY get_max(const XY& p, const XY& q) {
- return XY(maximum(p.x(), q.x()), maximum(p.y(), q.y()));
- }
- friend XY get_min(const XY& p, const XY& q) {
- return XY(minimum(p.x(), q.x()), minimum(p.y(), q.y()));
- }
- };
-
- #include "record.h"
-
- inline RECORD_STREAM& operator<<(RECORD_STREAM& rec, const XY& target)
- {
- char rec_str[100];
- sprintf(rec_str, "(%d, %d)", target.x(), target.y());
- rec << rec_str;
- return rec;
- }
-
- #endif /* _XY_H_ */
-